package com.rslai.base.tool.servertest.util;

import com.rslai.base.tool.servertest.model.KeyValueStore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Element;

public class XMLUtils {
    public static List<KeyValueStore> getAttribute(Element element) {
        List attributes = new ArrayList();
        Iterator iterator = element.attributeIterator();
        while (iterator.hasNext()) {
            Attribute attribute = (Attribute) iterator.next();
            String attributeName = attribute.getName();
            String attributeValue = attribute.getValue();
            attributes.add(new KeyValueStore(attributeName, attributeValue));
        }
        return attributes;
    }

    public static Map<String, String> getAttributeMap(Element element) {
        List attribute = getAttribute(element);
        return convertListKeyValueToMap(attribute);
    }

    public static Map<String, String> convertListKeyValueToMap(List<KeyValueStore> list) {
        Map map = new HashMap();
        for (KeyValueStore kvs : list) {
            map.put(kvs.getName(), (String) kvs.getValue());
        }
        return map;
    }
}
